
package com.comtop.test;

import java.io.File;
import java.nio.charset.Charset;

import cpdetector.io.CodepageDetectorProxy;
import cpdetector.io.JChardetFacade;

public class CharacterEnding {
    
    public static void main(String[] args) {
        String encoding = CharacterEnding.getFileCharacterEnding("src/describe.xml");
        System.out.println("encoding = " + encoding);
    }
    
    public static String getFileCharacterEnding(String filePath) {
        File file = new File(filePath);
        return getFileCharacterEnding(file);
    }
    
    public static String getFileCharacterEnding(File file) {
        
        String fileCharacterEnding = "UTF-8";
        cpdetector.io.CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
        detector.add(JChardetFacade.getInstance());
        
        Charset charset = null;
        try {
            charset = detector.detectCodepage(file.toURI().toURL());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (charset != null) {
            fileCharacterEnding = charset.name();
        }
        
        return fileCharacterEnding;
    }
}
